package Sequence;
import java.io.File;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class LoadedSequence implements ISequence{
	private static final long serialVersionUID = 1L;

	/**
	 * Charger un fichier midi via JFileChooser
	 */
	@Override
	public Sequence create() {
		JFileChooser chooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("MIDI files", "mid");
		File curentDirectory = new File(System.getProperty("user.dir"));
		
		chooser.setCurrentDirectory(curentDirectory); // Ouvrir la fenetre dans le dossier de l'application
		chooser.setFileFilter(filter);

		int returnVal = chooser.showOpenDialog(null);

		if(returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				File myMidiFile = new File(chooser.getSelectedFile().getAbsolutePath());
				Sequence mySeq;
				mySeq = MidiSystem.getSequence(myMidiFile);
				return mySeq;
			} catch (InvalidMidiDataException e) {
				System.out.println("ERROR: Invalid midi file.");
				return null;
			} catch (IOException e) {
				System.out.println("ERROR: Problems while opening file");
				return null;
			}
		}
		else{
			return null;
		}
	}
}
